﻿using System.Linq;

using Biz.EF.Gateway.Total;
using Biz.EF.Platform.Gxt;
using Hjt212Parser;
using log4net;

namespace Biz.Process
{
    public class CommonProcessor
    {
        private static ILog Log = LogManager.GetLogger(typeof(CommonProcessor));
        //public void Process(string cn)
        //{
        //    if (string.IsNullOrEmpty(cn)) return;
        //    using (var gCtx = new GatewayTotalContext())
        //    using (var pCtx = new PlatformGxtContext())
        //    {
        //        var flag = ProcessFlag.NotProcessed.ToString();
        //        var items =
        //            gCtx.received_msg.Where(t => t.is_length_right)
        //                .Where(t => t.processed_flag.Equals(flag))
        //                .Where(t => t.cn.Equals(cn))
        //                .OrderBy(t => t.datatime)
        //                .Take(1000)
        //                .ToList();
        //        foreach (var item in items)
        //        {
        //            Log.Info( "Processing " + cn + ":" + item.received_string);
        //            switch (item.cn)
        //            {
        //                case "2011":
        //                    ProcessorData.Process2011(pCtx, item.received_string);
        //                    break;
        //                case "2051":
        //                    ProcessorData.Process2051(pCtx, item.received_string);
        //                    break;
        //                case "2061":
        //                    ProcessorData.Process2061(pCtx, item.received_string);
        //                    break;
        //                case "2031":
        //                    ProcessorData.Process2031(pCtx, item.received_string);
        //                    break;
        //                case "0000":
        //                    ProcessorStatus.Process0000(pCtx, item.received_string);
        //                    break;
        //                case "2072":
        //                    ProcessorAlarm.Process2072(pCtx, item.received_string);
        //                    break;
        //            }
        //            if (!item.cn.Equals("0000"))
        //            {
        //                ProcessorStatus.Process9999(pCtx, item.received_string);
        //            }
        //            item.processed_flag = ProcessFlag.ProcessingFinished.ToString();
        //        }
        //        gCtx.SaveChanges();
        //    }
        //}

        public static void Process()
        {
            using (var gCtx = new GatewayTotalContext())
            using (var pCtx = new PlatformGxtContext())
            {
                var flag = ProcessFlag.NotProcessed.ToString();
                var items =
                    gCtx.received_msg.Where(t => t.is_length_right)
                        .Where(t => (t.processed_flag == null || t.processed_flag.Trim() == string.Empty) || t.processed_flag.Equals(flag))
                        .OrderBy(t => t.received_datetime)
                        .Take(1000)
                        .ToList();
                foreach (var item in items)
                {
                    Log.Info("Upline message processing:" + item.received_string);
                    switch (item.cn)
                    {
                        case "2011":
                            //ProcessorData.Process2011(pCtx, item.received_string);
                            break;
                        case "2051":
                            //ProcessorData.Process2051(pCtx, item.received_string);
                            break;
                        case "2061":
                            //ProcessorData.Process2061(pCtx, item.received_string);
                            break;
                        case "2031":
                            //ProcessorData.Process2031(pCtx, item.received_string);
                            break;
                        case "0000":
                            ProcessorStatus.Process0000(pCtx, item.received_string);
                            break;
                        case "2072":
                            ProcessorAlarm.Process2072(pCtx, item.received_string);
                            break;
                        case "9011":
                            ProcessorResponse.Process9011(pCtx, item.received_string);
                            break;
                        case "9012":
                            ProcessorResponse.Process9012(pCtx, item.received_string);
                            break;
                        case "9014":
                            ProcessorResponse.Process9014(pCtx, item.received_string);
                            break; ;
                        case "3081":
                            ProcessorStatus.Process3081(pCtx, item.received_string);
                            break;
                        case "3082":
                            ProcessorStatus.Process3082(pCtx, item.received_string);
                            break;
                        case "3071":
                            ProcessorStatus.Process3071(pCtx, item.received_string);
                            ProcessorStatus.Process3071_1(pCtx, item.received_string);
                            break;
                    }
                    if (!item.cn.Equals("0000"))
                    {
                        ProcessorStatus.Process9999(pCtx, item.received_string);
                    }
                    item.processed_flag = ProcessFlag.ProcessingFinished.ToString();
                }
                gCtx.SaveChanges();
            }
        }
    }
}
